
// set path here
gl path "~/"

use id year g_ homeowner_g value_decile using "$path/Replication/ohio_analysis_local.dta", clear

// rename homeowner variable
estimates clear
gen homeowner = homeowner_g
drop homeowner_g
label variable homeowner "Homeowner"

// column 1
eststo: reghdfe g_ homeowner, a(id year) cluster(id)
	sum g_ if e(sample)	
	estadd scalar avg = r(mean)
drop _est*

// do the matched diff in diff here

sort id year
** focus only on people eligible the entire time period
egen tmp = max(g_==.), by(id)
drop if tmp==1
drop tmp

// generate the treated group
by id: gen treated_group = (homeowner[1]==0 & homeowner[2]==0 & homeowner[3]==0 & ///
					homeowner[4]==0 & homeowner[5]==1)

// find control pool who aren't homeowners over same pre-treatment period
by id: gen control_pool = (homeowner[1]==0 & homeowner[2]==0 & homeowner[3]==0 & ///
					homeowner[4]==0 & homeowner[5]==0) 

keep if treated_group == 1 | control_pool == 1

// now we want to collapse to unique pre-treatment lagged outcome paths to do the matches
keep id year homeowner treated_group control_pool g_

** create string encoding of possible pre-treatment outcome paths
tostring g_, generate(outcome_path)

by id: gen outcome_path_pretreat = outcome_path[1] + outcome_path[2] + ///
								outcome_path[3] + outcome_path[4]
	
keep g_ homeowner id outcome_path_pretreat year
label variable homeowner "Homeowner"
// generate an outcome path fixed effect
egen op = group(outcome_path)
compress

// column 2
eststo: reghdfe g_ homeowner, a(id op year)
	sum g_ if e(sample)
	estadd scalar avg = r(mean)
	
	
use "$path/Replication/exact_matched.dta", clear
*** get weights for fw based on total n per stratum
*** each stratum has 18 obs
*** fw requires integers so need to round
gen tot2 = round(tot/18)
label variable homeowner "Homeowner"
egen op = group(outcome_path)

// column 3
eststo: reghdfe g_ homeowner [fw=tot2], a(op year)
	sum g_ [aw=tot] if e(sample)
	estadd scalar avg = r(mean)

use id year homeowner_p p_ voted_in_primary value_decile using "$path/Replication/ohio_analysis_local.dta", clear

// rename homeowner variable
gen homeowner = homeowner_p
drop homeowner_p
label variable homeowner "Homeowner"

// column 4
eststo: reghdfe voted_in_primary homeowner, a(id year) cluster(id)
	sum voted_in_primary if e(sample)	
	estadd scalar avg = r(mean)
drop _est*

// do the matched diff in diff here

sort id year
// recode p_ to be 1 if turned out, regardless of which party
replace p_ = 1 if p_ > 1 & p_ != .

** focus only on people eligible the entire time period
egen tmp = max(p_==.), by(id)
drop if tmp==1
drop tmp

by id: gen treated_group = (homeowner[1]==0 & homeowner[2]==0 & homeowner[3]==0 & ///
					homeowner[4]==1) 

// find control pool who aren't homeowners over same pre-treatment period
by id: gen control_pool = (homeowner[1]==0 & homeowner[2]==0 & homeowner[3]==0 & ///
					homeowner[4]==0) 

keep if treated_group == 1 | control_pool == 1

// now we want to collapse to unique pre-treatment lagged outcome paths to do the matches
keep id year homeowner treated_group control_pool p_ 

** create string encoding of possible pre-treatment outcome paths
tostring p_, generate(outcome_path)

by id: gen outcome_path_pretreat = outcome_path[1] + outcome_path[2] + ///
								outcome_path[3] + outcome_path[4] 
								
keep p_ homeowner id outcome_path_pretreat year		
					
label variable homeowner "Homeowner"
// generate outcome patch fixed effect
egen op = group(outcome_path)

// column 5
eststo: reghdfe p_ homeowner, a(id op year)
	sum p_ if e(sample)
	estadd scalar avg = r(mean)
drop _est*

use "$path/Replication/exact_matched_p.dta", clear
gen tot2 = round(tot/18)
label variable homeowner "Homeowner"
egen op = group(outcome_path)

// column 6
eststo: reghdfe p_ homeowner [fw=tot2], a(op year)
	sum p_ [aw=tot] if e(sample)
	estadd scalar avg = r(mean)

// print coefficients to tex file
esttab using "$path/Replication/Table1.tex", ///
substitute(\hline "\midrule" [1em] "" "avg" "Outcome Mean") tex ///
nostar  label  replace nonotes nomtitles nonumbers fragment scalar(N avg)  ///
b(%8.3f) se(%8.4f) sfmt(%12.0fc %4.3f) nolines varlabels( , elist(homeowner \addlinespace)) ///
compress

